Imports System.Data
Partial Class Admin_frmTabPurchase
    Inherits System.Web.UI.Page
    Dim _EmployeeId As String
    Dim dt As New DataTable
    Dim _PurchaseCartVO As New PurchaseCartVo
    Dim dt1 As New DataTable
    Dim dtPurId As New DataTable
    Dim _bo As New Bo
    Dim _PurchaseId As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        _EmployeeId = Session("EmpId")
        If _EmployeeId = "" Then
            Response.Redirect("../Client/frmLogin.aspx")
        End If
        If Not Page.IsPostBack Then
            Call ProductDetail()
            dt1 = _bo.getPriceByProductId(ddlProductId.Text)
            lblPrice.Text = dt1.Rows(0).Item(0).ToString
            img1.ImageUrl = dt1.Rows(0).Item(1).ToString
        End If
        dtPurId = _bo.getPurchaseId()
        _PurchaseId = Val(dtPurId.Rows(0).Item(0).ToString) + 1
        lblPurchaseId.Text = _PurchaseId
    End Sub
    Public Sub ProductDetail()
        dt = _bo.getProduct()
        ddlProductId.DataSource = dt
        ddlProductId.DataValueField = dt.Columns(0).ToString
        ddlProductId.DataTextField = dt.Columns(0).ToString
        ddlProductId.DataBind()
    End Sub
    Protected Sub ddlProductId_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlProductId.SelectedIndexChanged
        dt1 = _bo.getPriceByProductId(ddlProductId.Text)
        lblPrice.Text = dt1.Rows(0).Item(0).ToString
        img1.ImageUrl = dt1.Rows(0).Item(1).ToString
    End Sub
    Sub clearall()
        lblPrice.Text = ""
        txtQuantity.Text = ""
        ddlProductId.SelectedIndex = 0
    End Sub
    Protected Sub btnAddMore_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddMore.Click
        Dim dt1 As New DataTable

        Dim TotQty As Integer = 0
        dt1 = _bo.CheckProductInPurchaseCart(ddlProductId.Text)
        If dt1.Rows.Count = 0 Then
            _PurchaseCartVO.PurchaseId = lblPurchaseId.Text
            _PurchaseCartVO.ProductId = ddlProductId.Text
            _PurchaseCartVO.Quantity = txtQuantity.Text
            _PurchaseCartVO.Price = lblPrice.Text
            _bo.AddToPurchaseCart(_PurchaseCartVO)
            clearall()
        Else
            TotQty = Val(dt1.Rows(0).Item(0).ToString) + Val(txtQuantity.Text)
            _bo.UpdatePurchaseCart(ddlProductId.Text, TotQty)
            clearall()
        End If
    End Sub
    Protected Sub btnShowCart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnShowCart.Click
        Session("EmpId") = _EmployeeId
        Response.Redirect("../Admin/frmPurchaseCart.aspx?PurchaseId=" & lblPurchaseId.Text)
    End Sub
End Class
